package entity

import (
	"QImages/database"
	"github.com/jinzhu/gorm"
)

// Comment 评论，与数据库中comments表相对应
type Comment struct {
	gorm.Model
	UserID   uint
	ImageID  uint
	ParentID *uint
	Content  string
}

func init() {
	database.DB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").AutoMigrate(&Comment{})
}

// Insert 插入这条评论
func (comment Comment) Insert() (id uint, err error) {
	result := database.DB.Create(&comment)
	id = comment.ID
	if err = result.Error; err != nil {
		return
	}
	return
}

// Comments 获取所有评论
func Comments() (comments []Comment, err error) {
	results := database.DB.Find(&comments)
	if err = results.Error; err != nil {
		return
	}
	return
}

// DeleteComment 删除评论信息
func DeleteComment(id uint) (res Comment, err error) {
	res.ID = id
	if err = database.DB.Delete(&res).Error; err != nil {
		return
	}
	return
}
